package com.health.mapp.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.health.db.model.UserCollectFavorite;
import com.health.db.model.UserLIST;
import com.health.db.service.user.IUserCollectFavoriteService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.CollectDocReq;
import com.health.mapp.bean.CollectDocResp;
import com.health.mapp.bean.Header;
import com.health.mapp.enums.CollectionTypeEnum;
import com.health.mapp.utils.Response;

/**
 * Created by helili on 2017/2/20.
 */
public class CollectDocImpl implements ActionInterface {

    private Logger logger = Logger.getLogger(CollectDocImpl.class);

    private IUserCollectFavoriteService userCollectFavoriteService ;
	private IUserLISTService userLISTService  ;
    @Override
    public Response execute(Header header, Object reqBean) throws Exception {
        CollectDocReq req = (CollectDocReq) reqBean;
        CollectDocResp resp = new CollectDocResp();
        String userId= req.getUserId();
        String docId = req.getDocId();
        Integer collectType = req.getCollectType();
        logger.info("userId,docId,collectType:"+userId+","+docId+","+collectType);
        //参数校验
        if(StringUtils.isEmpty(userId)){
            resp.setErrorMsg("用户ID不能为空");
            resp.setErrorCode(-1);
            return resp;
        }
        if(StringUtils.isEmpty(docId)){
            resp.setErrorMsg("医生ID不能为空");
            resp.setErrorCode(-1);
            return resp;
        }
        if(null==collectType){
            resp.setErrorMsg("收藏类型不能为空");
            resp.setErrorCode(-1);
            return resp;
        }
        if(CollectionTypeEnum.valueOf(collectType)==null){
            resp.setErrorMsg("收藏类型有误");
            resp.setErrorCode(-1);
            return resp;
        }

        Long collectorId = Long.parseLong(userId);
        Long collectionId =  Long.parseLong(docId);
        UserCollectFavorite userCollectFavorite = userCollectFavoriteService.selectByCollectorIdAndCollectionId(collectorId,collectionId);
        UserLIST doctor = userLISTService.selectByUserId(collectionId);
        //收藏
        if(CollectionTypeEnum.SC.getValue()==collectType){
            if(userCollectFavorite != null){
                resp.setErrorMsg("您已收藏过该医生");
                resp.setErrorCode(0);
                return resp;
            }
            UserCollectFavorite newCollection = new UserCollectFavorite();
            newCollection.setCollectorid(collectorId);
            Date now = new Date();
            newCollection.setCollectDate(now);
            newCollection.setCollectionid(collectionId);
            newCollection.setCollectionName(doctor.getUserName());
            newCollection.setCreateDate(now);
            newCollection.setUpdateDate(now);
            newCollection.setUpdateUserid(collectorId);
            userCollectFavoriteService.insert(newCollection);
        }else if(CollectionTypeEnum.QXSC.getValue()==collectType){
            //取消收藏
            if(userCollectFavorite == null){
                resp.setErrorMsg("您未收藏过该医生");
                resp.setErrorCode(0);
                return resp;
            }
            userCollectFavoriteService.deleteByPrimaryKey(userCollectFavorite);
        }
        return resp;
    }
	 
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}

	public IUserCollectFavoriteService getUserCollectFavoriteService() {
		return userCollectFavoriteService;
	}

	public void setUserCollectFavoriteService(
			IUserCollectFavoriteService userCollectFavoriteService) {
		this.userCollectFavoriteService = userCollectFavoriteService;
	}
 
	
}
